package com.gtt.jr.app;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import com.gtt.jr.app.domain.TypeVO;
import com.gtt.jr.repository.domain.ContainerType;
import com.gtt.jr.service.TypeService;

/**
 * 集装箱类别仓库实现
 * 
 * @author 高甜甜(gao12581@sina.com)
 * @date 2010-8-29
 * 
 */
public class TypeRepositoryImpl implements TypeRepository {

	private static final Logger LOGGER = Logger
			.getLogger(TypeRepositoryImpl.class);

	@Resource
	private TypeService typeService;

	private List<TypeVO> types;

	@PostConstruct
	public void init() {
		List<ContainerType> types = typeService.findAll();
		this.types = new ArrayList<TypeVO>();
		for (ContainerType type : types) {
			this.types.add(convert(type));
		}
		LOGGER.info("初始化集装箱类别 - "
				+ StringUtils.collectionToCommaDelimitedString(this.types));
	}

	private TypeVO convert(ContainerType type) {
		TypeVO typeVO = new TypeVO();
		typeVO.setId(type.getId());
		typeVO.setName(type.getName());
		return typeVO;
	}

	public List<TypeVO> findAll() {
		return types;
	}

	public TypeVO get(Long id) {
		int index = types.indexOf(new TypeVO(id));
		if (index != -1) {
			return types.get(types.indexOf(new TypeVO(id)));
		} else {
			return new TypeVO();
		}
	}
}
